<?php
namespace app\common\library;
class MyDateTime
{
    private $timestamp;

    public function __construct($dateStr = 'now')
    {
        $this->timestamp = strtotime($dateStr);
    }

    public function format($format="Y-m-d H:i:s")
    {
        return date($format, $this->timestamp);
    }

    public function modify($modifyStr)
    {
        $newTimestamp = strtotime($modifyStr, $this->timestamp);
        if ($newTimestamp === false) {
            throw new Exception("Invalid modification string: {$modifyStr}");
        }
        $this->timestamp = $newTimestamp;
        return $this;
    }

    public function getTimestamp()
    {
        return $this->timestamp;
    }

    public function __get($name)
    {
        switch ($name) {
            case 'year':
                return (int)date('Y', $this->timestamp);
            case 'month':
                return (int)date('n', $this->timestamp);
            case 'day':
                return (int)date('j', $this->timestamp);
            case 'hour':
                return (int)date('G', $this->timestamp);
            case 'minute':
                return (int)date('i', $this->timestamp);
            case 'second':
                return (int)date('s', $this->timestamp);
            default:
                throw new Exception("Invalid property: {$name}");
        }
    }
}